package com.hcbd.tdwt.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import static android.content.Context.SENSOR_SERVICE;

/**
 * - @Description:  指北针
 * - @Author:  LXJ
 * - @Time:  2019/3/14 11:31
 */
public class CompassView extends View implements SensorEventListener {

    private int mWidth;
    private int mHeigth;
    private Sensor sensor;
    private SensorManager sensorManager;
    private int toDegrees;

    public void init() {
        if (sensorManager == null) {
            sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
            //通过 getDefaultSensor 获得指南针传感器
            sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
            //为传感器管理者注册监听器，第三个参数指获取速度正常
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
        }
    }

    //红色文字笔
    private Paint mRedTextPaint;
    //白色文字笔
    private Paint mWhiteTextPaint;
    //大菊花笔
    private Paint mWhiteScalePaint;
    //小菊花笔
    private Paint mScalePaint;
    private Context context;

    public CompassView(Context context) {
        this(context, null);
    }

    public CompassView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CompassView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        //北方向的红色画笔
        mRedTextPaint = new Paint();
        mRedTextPaint.setDither(true);
        mRedTextPaint.setAntiAlias(true);
        mRedTextPaint.setColor(Color.RED);
        mRedTextPaint.setTextSize(40);

        //白色画笔
        mWhiteTextPaint = new Paint();
        mWhiteTextPaint.setDither(true);
        mWhiteTextPaint.setAntiAlias(true);
        mWhiteTextPaint.setColor(Color.WHITE);
        mWhiteTextPaint.setStyle(Paint.Style.FILL_AND_STROKE );
        mWhiteTextPaint.setTextSize(40);
        mWhiteTextPaint.setStrokeCap(Paint.Cap.ROUND);
        mWhiteTextPaint.setStrokeJoin(Paint.Join.ROUND);

        mWhiteScalePaint = new Paint();
        mWhiteScalePaint.setDither(true);
        mWhiteScalePaint.setAntiAlias(true);
        mWhiteScalePaint.setColor(Color.WHITE);
        mWhiteScalePaint.setStyle(Paint.Style.FILL_AND_STROKE );
        mWhiteScalePaint.setStrokeWidth(10);
        mWhiteScalePaint.setStrokeCap(Paint.Cap.ROUND);
        mWhiteScalePaint.setStrokeJoin(Paint.Join.ROUND);

        mScalePaint = new Paint();
        mScalePaint.setDither(true);
        mScalePaint.setAntiAlias(true);
        mScalePaint.setColor(Color.GRAY);
        mScalePaint.setStyle(Paint.Style.FILL_AND_STROKE );
        mScalePaint.setStrokeWidth(5);
        mScalePaint.setStrokeCap(Paint.Cap.ROUND);
        mScalePaint.setStrokeJoin(Paint.Join.ROUND);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeigth = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeigth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int x = mWidth / 2;
        int y = mHeigth / 2;
        int r = (int) (mWidth * 0.42);
        canvas.save();
        for (int i = 0; i < 120; i++) {
            if (i == 0) {
                drawDegrees(i * 3 + "", canvas);
                drawText("北", canvas, mRedTextPaint);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.12), mWhiteScalePaint);
            } else if (i == 30) {
                drawDegrees(i * 3 + "", canvas);
                drawText("东", canvas, mWhiteTextPaint);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mWhiteScalePaint);
            } else if (i == 60) {
                drawDegrees(i * 3 + "", canvas);
                drawText("南", canvas, mWhiteTextPaint);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mWhiteScalePaint);
            } else if (i == 90) {
                drawDegrees(i * 3 + "", canvas);
                drawText("西", canvas, mWhiteTextPaint);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mWhiteScalePaint);
            } else if (i == 10 || i == 20 || i == 40 || i == 50 || i == 70 || i == 80 || i == 100 || i == 110) {
                drawDegrees(i * 3 + "", canvas);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mWhiteScalePaint);
            } else if (i % 2 == 0) {
                //绘制下层菊花
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mScalePaint);
            }

            canvas.rotate(360 / 120, x, y);
        }
        canvas.restore();
    }

    /**
     * 绘制文字
     *
     * @param string
     * @param canvas
     */
    public void drawText(String string, Canvas canvas, Paint paint) {
        String text = string;
        Rect textRect = new Rect();
        paint.getTextBounds(text, 0, text.length(), textRect);
        int startX = getWidth() / 2 - textRect.width() / 2;
        int baseLine = 150;
        canvas.drawText(text, startX, baseLine, paint);
    }

    /**
     * 绘制度数
     *
     * @param string
     * @param canvas
     */
    public void drawDegrees(String string, Canvas canvas) {
        String text = string;
        Rect textRect = new Rect();
        mWhiteTextPaint.getTextBounds(text, 0, text.length(), textRect);
        int startX = getWidth() / 2 - textRect.width() / 2;
        int textHeight = textRect.height();
        int baseLine = 0;
        Paint.FontMetricsInt fontMetricsInt = mWhiteTextPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        baseLine = textHeight / 2 + dy;
        canvas.drawText(text, startX, baseLine, mWhiteTextPaint);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        switch (sensorEvent.sensor.getType()) {
            case Sensor.TYPE_ORIENTATION:
                //顺时针转动为正，故手机顺时针转动时，图片得逆时针转动
                toDegrees = (int) -sensorEvent.values[0];
                if (this.onAnglaChanged != null) {
                    onAnglaChanged.onChanged(toDegrees);
                }
                break;
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (sensorManager != null) {
            sensorManager.unregisterListener(this);
        }
    }

    public interface OnAnglaChanged {
        void onChanged(int angla);
    }

    public OnAnglaChanged onAnglaChanged;

    public void setOnAnglaChanged(OnAnglaChanged onAnglaChanged) {
        this.onAnglaChanged = onAnglaChanged;
    }
}
